package com.cea.papyrus.activitydiagram.tabbedproperties.composite;

import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.jface.text.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.uml2.uml.InputPin;
import org.eclipse.uml2.uml.InvocationAction;

import com.cea.papyrus.core.commands.MoveInListCommand;
import com.cea.papyrus.core.commands.UMLElementCreateCommand;
import com.cea.papyrus.core.commands.factories.UMLDi2ElementDeleteFactory;
import com.cea.papyrus.core.editor.CommandStackUtils;
import com.cea.papyrus.core.utils.UMLObjectLabelProvider;
import com.cea.papyrus.tabbedproperties.composites.DecoratedTableComposite;
import com.cea.papyrus.tabbedproperties.composites.ISectionComposite;
import com.cea.papyrus.umlutils.Element;

public class ArgumentComposite extends DecoratedTableComposite implements ISectionComposite {

	private InvocationAction invocationAction;
	/**
	 * key to access UML Data from table items.
	 */ 
	private final static String KEY = "argument_data";
	public ArgumentComposite() {
		super("Arguments");
	}

	@Override
	public void refresh() {
		if( !table.isDisposed()&& (invocationAction!=null)){
			// Remove parameters from table 
			table.removeAll();

			// Read and add parameter
			Iterator<InputPin> it = invocationAction.getArguments().iterator();
			while (it.hasNext()) {
				InputPin argument = it.next();
				TableItem item = new TableItem(table, SWT.BORDER);

				UMLObjectLabelProvider labelProvider = new UMLObjectLabelProvider();
				item.setImage(labelProvider.getImage(argument));
				if (argument.getType()!=null){
					item.setText(argument.getName()+":"+argument.getType().getLabel());
				}
				else{item.setText(argument.getName());}
				item.setData(KEY, argument);
			}
		}
	}

	@Override
	public void setElement(Element element) {
		super.setElement(element);
		this.invocationAction=(InvocationAction)element.getUml2Element();
	}

	@Override
	public void downButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();

		for(int i = (tableItems.length-1); i >= 0; i--) {
			// get argument list
			List arguments = invocationAction.getArguments();
			// get argument
			org.eclipse.uml2.uml.InputPin inputPin 
			= ((InputPin) tableItems[i].getData(KEY));
			int newIndex = table.indexOf(tableItems[i]) + 1;
			if(newIndex < arguments.size()) {
				//new MoveInListCommand(parameters, parameter, newIndex).execute();
				compoundCommand.add(new MoveInListCommand(arguments, inputPin, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);

	}

	@Override
	public void editItem(TableItem item) {
		/*TODO: TO to fill*/

	}

	public void addButtonPressed() {
		Command command = new UMLElementCreateCommand(org.eclipse.uml2.uml.InputPin.class, invocationAction);
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(command);
	}

	@Override
	public void removeButtonPressed() {
		CompoundCommand command = new CompoundCommand(); 
		// get selection and delete it
		TableItem[] tableItems = table.getSelection();
		for(int i = 0; i < tableItems.length ; i++) {
			// get parameter
			InputPin inputPin = (InputPin) tableItems[i].getData(KEY);
			command.add(UMLDi2ElementDeleteFactory.eINSTANCE.createCommand(inputPin));
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(command);

	}

	@Override
	public void upButtonPressed() {
		CompoundCommand compoundCommand = new CompoundCommand();
		TableItem[] tableItems = table.getSelection();

		for(int i = (tableItems.length-1); i >= 0; i--) {
			// get argument list
			List arguments = invocationAction.getArguments();
			// get argument
			org.eclipse.uml2.uml.InputPin inputPin 
			= ((InputPin) tableItems[i].getData(KEY));
			int newIndex = table.indexOf(tableItems[i]) - 1;
			if(newIndex >-1) {
				//new MoveInListCommand(parameters, parameter, newIndex).execute();
				compoundCommand.add(new MoveInListCommand(arguments, inputPin, newIndex));
			}
		}
		CommandStack stack = CommandStackUtils.getCommandStack();
		Assert.isNotNull(stack, "Impossible to adapt current editor into a CommandStack");
		stack.execute(compoundCommand);

	}

}
